<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{ $route.meta.titie }}</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索框 -->
      <div style="margin-top: 15px">
        <el-input placeholder="请输入内容" v-model="page.query" class="input-with-select">
          <el-button slot="append" @click="searchbox" icon="el-icon-search"></el-button></el-input>
        <el-button class="add" type="primary" @click="add">添加用户</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="userlist" border style="width: 100%">
        <el-table-column prop="id" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role-name" label="角色"> </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="status(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
          <template slot-scope="scope">
            <el-button
            @click="edit(scope.row)"
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              @click="del(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
            ></el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-s-tools"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- urrent-page 当前页数 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="40%">
      <!-- 添加表单 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="off">取消</el-button>
          <el-button @click="submitForm('ruleForm')">添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog
      title="编辑用户" :visible.sync="dialogVisibleedit"  width="50%">
      <el-form :model="formedit" :rules="rulesedit" ref="formedit" label-width="100px" class="demo-ruleForm">
     <el-form-item label="用户名" prop="username">
      <el-input  :disabled="true" v-model="formedit.username"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="formedit.email"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formedit.mobile"></el-input>
    </el-form-item>
     <el-form-item size="large">
    <el-button type="primary" @click="editadd">立即创建</el-button>
    <el-button>取消</el-button>
  </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getStatus, getUserlist, getAdd, getDel,getEdit,getSearch } from "../../utils/api";
export default {
  data() {
    return {
      id:1,
      // 添加表单
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
      rulesedit:{
         email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
          ],
         mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
          ],
      },
      formedit:{ //编辑表单
      username:"",
      email:"",
      mobile:""
      },
      dialogVisibleedit:false,//编辑对话框显示隐藏
      dialogVisible: false, //添加对话框的显示与隐藏
      value: true, //状态开关
      userlist: [],
      total: 10, //总条数
      page: {
        query: "",//搜索内容
        pagenum: 1, //当前页码数
        pagesize: 10, //每页条数
      },
    };
  },
  methods: { 
    searchbox(){ //搜索
     this.rander();
   
    },
    editadd(){//编辑中的确定按钮
    console.log(this.formedit)
      getEdit(this.formedit).then(res=>{
                console.log(res);
        if(res.data.meta.status==200){
          this.$message({
            type: 'success', // success error warning
            message: res.data.meta.msg,
            duration: 2000,
          })
          this.rander()
          this.dialogVisibleedit=false
        }
      })
    },
    edit(row){//编辑
      this.dialogVisibleedit=true
      // console.log(username);
      this.formedit={...row}
      this.id=row.id
    },
    rander() {
      //封装用户列表
      getUserlist(this.page).then((res) => {
        //请求用户数据
        // console.log(res);
        this.userlist = res.data.data.users;
        this.total = res.data.data.total;
      });
    },
    handleSizeChange(val) {
      //每页条数
      console.log(`每页 ${val} 条`);
      this.page.pagesize = val;
      this.rander();
    },
    handleCurrentChange(val) {
      //当前页码
      console.log(`当前页: ${val}`);
      this.page.pagenum = val;
      this.rander();
    },
    status(row) {
      //点击修改用户状态
      console.log(row);
      getStatus(row.id, row.mg_state).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.$message({
            type: "success", // success error warning
            message: res.data.meta.msg,
            duration: 2000,
          });
        }
      });
    },
    add() {
      //添加按钮
      this.dialogVisible = true;
    },
    submitForm(formName) {
      //添加模态框的确定按钮
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          getAdd(this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status == 201) {
              this.$message({
                type: "success", // success error warning
                message: res.data.meta.msg,
                duration: 2000,
              }),
                (this.ruleForm = {
                  username: "",
                  password: "",
                  email: "",
                  mobile: "",
                });
            }
            this.rander();
            this.dialogVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    off() {
      //添加模态框的取消按钮
      this.dialogVisible = false;
    },
    del(id) {
      //删除
      getDel(id).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.$message({
            type: "success", // success error warning
            message: res.data.meta.msg,
            duration: 2000,
          });
          this.rander();
        }
      });
    },
  },
  created() {
    this.rander();
  },
};
</script>
<style lang="scss" scoped>
.box-card {
  margin-top: 20px;
  .el-input {
    width: 500px;
  }
  .add {
    margin-left: 20px;
  }
}
</style>